﻿using System;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ProjectManager.Masters.ModuleData;
using ApplicationLibrary.SqlConnector;

namespace ProjectManager.Masters.Forms
{
    public partial class Bugs : ApplicationLibrary.DBManager.DocumentForm, iDocument
    {
        private BindableOject<int> bdoProjectType = new BindableOject<int>();
        private EnumsManager<Status> StatusManager = new EnumsManager<Status>(Properties.Resources.ResourceManager);
        private PartialCounter CounterManager = new PartialCounter();
        private RRProjects rrProjects = null;

        #region Interface
        public ApplicationLibrary.ExtendedControls.ExTabControl masterTabControl { get { return exTabControl1; } }
        public Counters ProjectType { get { return Counters.Bug; } }
        public int ProjectNumb { get { return cntNumber.CounterNumericValue; } }
        public string Description { get { return txtDescription.Text; } }
        #endregion

        public Bugs()
            : base("Bugs")
        {
            InitializeComponent();
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerProjects("Bugs", new RadarProjects(Counters.Bug), this);
            dbManager.AddMaster(this, PM_Project.Name, true);

            cntNumber.AttachCounterType(Counters.Bug.Int(), GlobalInfo.CurrentDate, null);
            cbbProduct.AttachDataReader<string>(new RRDescriptions(DescriptionType.E_Products), PM_Descriptions.Code, PM_Descriptions.Description);
            cbbVersion.AttachDataReader<string>(new RRDescriptions(DescriptionType.E_Versions), PM_Descriptions.Code, PM_Descriptions.Description);

            StatusManager.AttachTo(cbbStatus);
            CounterManager.AttachTo(cbbFatherType);

            base.OnAttachData();
        }

        protected override void OnBindData()
        {
            BindControl(cntNumber, PM_Project.Code, NullValue.NotSet, Findable.YES);
            BindControl(ctbDate, PM_Project.Date);
            BindControl(txtDescription, PM_Project.Description);
            BindControl(cbbProduct, PM_Project.Product, "SelectedValue", NullValue.NotSet);
            BindControl(cbbVersion, PM_Project.Version, "SelectedValue", NullValue.NotSet);
            BindControl(cbbStatus, PM_Project.Status, "SelectedValue", NullValue.NotSet);
            BindControl(ctbStarted, PM_Project.StartingDate);
            BindControl(ctbEnded, PM_Project.EndingDate);
            BindControl(rtbNote, PM_Project.Note, "Rtf");
            BindControl(cbbFatherType, PM_Project.FatherType, "SelectedValue", NullValue.NotSet);
            BindControl(cbbFather, PM_Project.Father, "SelectedValue", NullValue.NotSet);

            BindObject(bdoProjectType, PM_Project.Type);
            BindCounter(cntNumber);
            base.OnBindData();
        }

        protected override bool OnAfterAddNew()
        {
            cbbStatus.SelectedValue = Status.ToDo.Long();
            bdoProjectType.Value = Counters.Bug.Int();
            ctbDate.Today = GlobalInfo.CurrentDate;

            return base.OnAfterAddNew();
        }

        protected override void OnDisableControlsForEdit()
        {
            cntNumber.Enabled = false;
            base.OnDisableControlsForEdit();
        }

        private void cbbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            rrProjects = new RRProjects(CounterManager.GetValue());
            if (GlobalInfo.LoginInfo.ProviderType == ProviderType.SQLite)
                cbbFather.AttachDataReader<long>(rrProjects, PM_Project.Code, PM_Project.Description);
            else
                cbbFather.AttachDataReader<int>(rrProjects, PM_Project.Code, PM_Project.Description);
        }

        private class PartialCounter : EnumsManager<Counters>
        {
            public override bool DisplayValue(Counters t)
            {
                return t != Counters.Bug;
            }
        }
    }
}